package interviewSearch

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/interviewSearch"
	interviewSearchReq "github.com/flipped-aurora/gin-vue-admin/server/model/interviewSearch/request"
)

type InterviewSearchService struct {
}

// CreateInterviewSearch 创建interviewSearch表记录
// Author [piexlmax](https://github.com/piexlmax)
func (interviewSearchsService *InterviewSearchService) CreateInterviewSearch(interviewSearchs *interviewSearch.InterviewSearch) (err error) {
	err = global.GVA_DB.Create(interviewSearchs).Error
	return err
}

// DeleteInterviewSearch 删除interviewSearch表记录
// Author [piexlmax](https://github.com/piexlmax)
func (interviewSearchsService *InterviewSearchService) DeleteInterviewSearch(interviewSearchs interviewSearch.InterviewSearch) (err error) {
	err = global.GVA_DB.Delete(&interviewSearchs).Error
	return err
}

// DeleteInterviewSearchByIds 批量删除interviewSearch表记录
// Author [piexlmax](https://github.com/piexlmax)
func (interviewSearchsService *InterviewSearchService) DeleteInterviewSearchByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]interviewSearch.InterviewSearch{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateInterviewSearch 更新interviewSearch表记录
// Author [piexlmax](https://github.com/piexlmax)
func (interviewSearchsService *InterviewSearchService) UpdateInterviewSearch(interviewSearchs interviewSearch.InterviewSearch) (err error) {
	err = global.GVA_DB.Save(&interviewSearchs).Error
	return err
}

// GetInterviewSearch 根据id获取interviewSearch表记录
// Author [piexlmax](https://github.com/piexlmax)
func (interviewSearchsService *InterviewSearchService) GetInterviewSearch(id uint) (interviewSearchs interviewSearch.InterviewSearch, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&interviewSearchs).Error
	return
}

// GetInterviewSearchInfoList 分页获取interviewSearch表记录
// Author [piexlmax](https://github.com/piexlmax)
func (interviewSearchsService *InterviewSearchService) GetInterviewSearchInfoList(info interviewSearchReq.InterviewSearchSearch) (list []interviewSearch.InterviewSearch, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&interviewSearch.InterviewSearch{})
	var interviewSearchss []interviewSearch.InterviewSearch
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.CompanyName != "" {
		db = db.Where("company_name = ?", info.CompanyName)
	}
	if info.InterviewTitle != "" {
		db = db.Where("interview_title = ?", info.InterviewTitle)
	}
	if info.Level1 != "" {
		db = db.Where("level1 = ?", info.Level1)
	}
	if info.Level2 != "" {
		db = db.Where("level2 = ?", info.Level2)
	}
	if info.Level3 != "" {
		db = db.Where("level3 = ?", info.Level3)
	}
	if info.TagContent != "" {
		db = db.Where("tag_content = ?", info.TagContent)
	}

	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&interviewSearchss).Error
	return interviewSearchss, total, err
}
